package com.lambda;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class LambdaZip {

    public static void printEntries(String zip, PrintStream out) {
        try (ZipFile zipFile = new ZipFile(zip)) {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            List<String> names = new ArrayList<>();
            while (entries.hasMoreElements()) {
                ZipEntry zipEntry = entries.nextElement();
                if (!zipEntry.isDirectory() && zipEntry.getName().endsWith(".js")) {
                    names.add(zipEntry.getName());
                }
            }
            // names.sort(Comparator.comparing(x -> x.toString()));
            names.sort(new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    return o1.compareTo(o2);
                }
            });
            // names.forEach(out::println);
            for (String name : names) {
                out.println(name);
            }
        } catch (Exception e) {

        }
    }

    public static void printEntries(String zip) {
        try (ZipFile zipFile = new ZipFile(zip)) {
            zipFile.stream().filter(x -> !x.isDirectory() && x.getName().endsWith(".js")).map(x -> x.getName()).sorted()
                    .forEach(System.out::println);
        } catch (Exception e) {

        }
    }

    public static void main(String[] args) {
        String zip = "D:/git/friday20161220.zip";
        long s = System.currentTimeMillis();
        printEntries(zip, System.out);
        long e = System.currentTimeMillis();
        System.out.println(e - s);

        System.out.println("===========================");

        long ss = System.currentTimeMillis();
        printEntries(zip);
        long ee = System.currentTimeMillis();
        System.out.println(ee - ss);

    }

}
